
package spikes_activity

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/spikes_activity"
    spikes_activityReq "github.com/flipped-aurora/gin-vue-admin/server/model/spikes_activity/request"
)

type SpikesActivitiesService struct {}
// CreateSpikesActivities 创建spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService) CreateSpikesActivities(spikesActivities *spikes_activity.SpikesActivities) (err error) {
	err = global.GVA_DB.Create(spikesActivities).Error
	return err
}

// DeleteSpikesActivities 删除spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService)DeleteSpikesActivities(ID string) (err error) {
	err = global.GVA_DB.Delete(&spikes_activity.SpikesActivities{},"id = ?",ID).Error
	return err
}

// DeleteSpikesActivitiesByIds 批量删除spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService)DeleteSpikesActivitiesByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]spikes_activity.SpikesActivities{},"id in ?",IDs).Error
	return err
}

// UpdateSpikesActivities 更新spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService)UpdateSpikesActivities(spikesActivities spikes_activity.SpikesActivities) (err error) {
	err = global.GVA_DB.Model(&spikes_activity.SpikesActivities{}).Where("id = ?",spikesActivities.ID).Updates(&spikesActivities).Error
	return err
}

// GetSpikesActivities 根据ID获取spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService)GetSpikesActivities(ID string) (spikesActivities spikes_activity.SpikesActivities, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&spikesActivities).Error
	return
}
// GetSpikesActivitiesInfoList 分页获取spikesActivities表记录
// Author [yourname](https://github.com/yourname)
func (spikesActivitiesService *SpikesActivitiesService)GetSpikesActivitiesInfoList(info spikes_activityReq.SpikesActivitiesSearch) (list []spikes_activity.SpikesActivities, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&spikes_activity.SpikesActivities{})
    var spikesActivitiess []spikes_activity.SpikesActivities
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.StartCreatedAt !=nil && info.EndCreatedAt !=nil {
     db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&spikesActivitiess).Error
	return  spikesActivitiess, total, err
}
func (spikesActivitiesService *SpikesActivitiesService)GetSpikesActivitiesPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
